/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.common.logger;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.AbstractMatcherFilter;
import ch.qos.logback.core.spi.FilterReply;


// 项目启动时，仅记录项目启动日志的日志拦截器实现
// <p>logback.xml中增加形如：
//     <appender name="bootstrapAppender" class="ch.qos.logback.core.FileAppender">
//         <file>${log.base}/bootstrap.log</file>
//         <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
//             <pattern>#s_logger#%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}-%line %msg #e_logger#%n</pattern>
//         </encoder>
//         <filter class="yhao.infra.common.logger.BootstrapLogDoneFilter">
//             <onMatch>ACCEPT</onMatch>
//             <onMismatch>DENY</onMismatch>
//         </filter>
//     </appender>
// <p></p>
/**
 * <p>启动类在启动完后增加代码：BootstrapLogDoneFilter.bootstrapDone();
 * @author yoara
 */
public class BootstrapLogDoneFilter extends AbstractMatcherFilter<ILoggingEvent> {
    private static boolean bootstrapDone = false;

    public static void bootstrapDone() {
        bootstrapDone = true;
    }

    @Override
    public FilterReply decide(ILoggingEvent event) {
        if (bootstrapDone) {
            return onMismatch;
        } else {
            return onMatch;
        }
    }
}
